﻿using CommHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Security;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace NX5WinServies
{
    public class EverydayCheck
    {
        public static void CheckAccountPreoid()
        {
            try
            {
                DataManager dm = new DataManager();
                dm.Procedure("CheckAccountPeriod", "[]");
            }catch(Exception ex)
            {
                Log.Write("Day", ex.ToString());
            }
        }

        public static void CheckLessonEvl()
        {
            try
            {
                DataManager dm = new DataManager();
                DataTable dt = dm.SqlCmd("select K.ID,K.UUID,COUNT(CLE.ID) Cnt from( "
                    + " select ID, UUID, cast(CONVERT(VARCHAR(10), studyday, 120) + ' ' + convert(varchar(8), starttime) as datetime) StudyDay from "
                    + " Cus_Lesson where Status = 5) K "
                    + " left JOIN Cus_Lesson_Evaluate CLE ON CLE.LessonUUID = K.UUID "
                    + " WHERE DATEADD(D,2,K.StudyDay) < getdate() "
                    + " group by K.ID,K.UUID "
                    + " having COUNT(CLE.ID) < 2").table;

                Console.WriteLine(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    Console.WriteLine(dr["ID"].ToString());
                    DataTable Evldt = dm.SqlCmd("select * from Cus_Lesson_Evaluate where LessonUUID = '" + dr["UUID"].ToString() + "'").table;
                    if (Evldt.Rows.Count > 0)
                    {
                        if (Evldt.Select("Type = 1").Length == 0)
                        {
                            dm.SqlCmd("insert into Cus_Lesson_Evaluate(LessonUUID,Evaluate,Type,EvaluateScore) values('" + dr["UUID"].ToString() + "','',1,5)");
                        }
                        if (Evldt.Select("Type = 2").Length == 0)
                        {
                            dm.SqlCmd("insert into Cus_Lesson_Evaluate(LessonUUID,Evaluate,Type,EvaluateScore) values('" + dr["UUID"].ToString() + "','',2,5)");
                        }
                    }
                    else
                    {
                        dm.SqlCmd("insert into Cus_Lesson_Evaluate(LessonUUID,Evaluate,Type,EvaluateScore) values('" + dr["UUID"].ToString() + "','',1,5)");
                        dm.SqlCmd("insert into Cus_Lesson_Evaluate(LessonUUID,Evaluate,Type,EvaluateScore) values('" + dr["UUID"].ToString() + "','',2,5)");
                    }

                    dm.SqlCmd("update Cus_Lesson set Status = 6 where UUID = '" + dr["UUID"].ToString() + "'");
                }
            }
            catch (Exception ex)
            {
                Log.Write("Day", ex.ToString());
            }
        }
        

        public static void CheckOuttimeLesson()
        {
            try
            {
                DataManager dm = new DataManager();
                dm.Procedure("Lessonouttime", "[]");
            }
            catch (Exception ex)
            {
                Log.Write("Day", ex.ToString());
            }
        }

        public static void AutoSettle()
        {
            try
            {
                DataManager dm = new DataManager();
                DataTable dt = dm.SqlCmd("SELECT * FROM Cus_TeacherInfo").table;
                foreach(DataRow dr in dt.Rows)
                {
                    dm.Procedure("AutoSettle", "[{\"TeacherId\":" + dr["ID"].ToString() + "}]");
                }
                
            }
            catch (Exception ex)
            {
                Log.Write("Day", ex.ToString());
            }
        }
    }
}
